package team.wuyou.service.imp;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import team.wuyou.bean.Result;
import team.wuyou.dao.BoatMapper;
import team.wuyou.service.BoatService;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
@Service
public class BoatServiceImp implements BoatService {

    @Autowired
    BoatMapper boatMapper;
    @Override
    public Result SearchBoat(Map map) {
        Result result = new Result();
        if (!map.get("location").equals("")&& map.get("location")!=null){
//            判断page是否为空
            if (map.get("location").equals("")&& map.get("location")==null){
//                为空则替换成0
                map.replace("page",0);
            }
            try {
//                检验是否为数字，不是数字则会转换失败，异常被处理
                int page = Integer.parseInt(map.get("page").toString())-1;
//                页数
                map.replace("page",page*9);
            }catch (Exception e){
                System.out.println();
                result.setCode("0");
                result.setMsg("页数只能为数字");
                return result;
            }
            //搜索用户信息
            List<Map> data = boatMapper.SearchBoat(map);
            //获取船只航线信息
            List list = SearchBoatRoute(data);
            if (data.size()>0){
                String countBoat = boatMapper.CountBoat(map);
                result.setCount(countBoat);
                result.setCode("1");
                result.setData(list);
                result.setMsg("查询成功");
            }else {
                result.setCode("0");
                result.setMsg("未找到该城市船只信息");
            }
        }else {
            //为空
            result.setCode("0");
            result.setMsg("地址为空");
        }
        return result;
    }
    @Override
    public Result MoreBoat(Map map) {
        Result result = new Result();
        if (!map.get("location").equals("")&& map.get("location")!=null){
//            判断page是否为空
            if (map.get("location").equals("")&& map.get("location")==null){
//                为空则替换成0
                map.replace("page",0);
            }
            try {
//                检验是否为数字，不是数字则会转换失败，异常被处理
                int page = Integer.parseInt(map.get("page").toString())-1;
//                页数
                map.replace("page",page*9);
            }catch (Exception e){
                System.out.println();
                result.setCode("0");
                result.setMsg("页数只能为数字");
                return result;
            }
            //根据前台信息选择执行哪条sql
            //条件 信息
            Condition(map);
            //搜索用户信息
            List<Map> data = boatMapper.MoreBoat(map);
            //获取船只航线信息
            List list = SearchBoatRoute(data);
            if (data.size()>0){
                result.setCode("1");
                result.setData(list);
                result.setMsg("查询成功");
            }else {
                result.setCode("0");
                result.setMsg("未找到该城市船只信息");
            }
        }else {
            //为空
            result.setCode("0");
            result.setMsg("地址为空");
        }
        return result;
    }

    //查询航线信息
    public List SearchBoatRoute(List<Map> data) {

        for (int i = 0; i < data.size(); i++) {
//            获取船只id
            String boat_id = (Integer) data.get(i).get("boat_id")+"";
//            根据船只id查询航线信息
            List<Map> boatRoute = boatMapper.SearchBoatRoute(boat_id);
//            将订单信息更新进船只信息中
            data.get(i).put("boatRoute",boatRoute);
        }
        return data;
    }
//条件sql
    public void Condition(Map map){
        String condition = (String)map.get("condition");
        if (condition.equals("1")) {

        }
        else if (condition.equals("2")) {

        }
        else if (condition.equals("3")) {

        }
        else if (condition.equals("4")) {

        }
        else if (condition.equals("5")) {

        }

        map.get("");
    }



}
